package com.acme.bankapp.service.bank;

import com.acme.bankapp.domain.bank.*;
import com.acme.bankapp.domain.bank.exceptions.*;

import java.io.File;
import java.util.Random;

/**
 * Created by anton on 8/18/14.
 */
public class BankApplication {
    public static void createAccounts(Bank bank) {
        Random random = new Random();
        for (int i=0; i<bank.getMaxClients()/2; i++) {
            try {
                BankService.addClient(bank, new Client(
                        "Client",
                        Gender.MALE,
                        new SavingsAccount(Math.round(random.nextDouble()*100))));
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (ClientExistsException e) {
                System.out.println("Client " + e.getClientName() + " already exists.");
            }
        }
        for (int i=bank.getMaxClients()/2; i<bank.getMaxClients(); i++) {
            try {
                BankService.addClient(bank, new Client(
                        "Client" + random.nextInt(100),
                        Gender.MALE,
                        new CheckingAccount(Math.round(random.nextDouble()*100),
                                Math.round(random.nextDouble()*100))));
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (ClientExistsException e) {
                System.out.println("Client " + e.getClientName() + " already exists.");
            }
        }
    }

    public static void modifyBank(Bank bank) {
        Random random = new Random();

        for (Client c : bank.getClients()) {
            try {
                AbstractAccount acc = c.getAccount();
                acc.deposit(Math.round(random.nextDouble() * 100));
                acc.withdraw(Math.round(random.nextDouble() * 100));
            } catch (NotEnoughFundsException e) {
                System.out.println("Not Enough Funds: " + e.getAmount());
            }
        }

    }

    public static void printBalance(Bank bank) {
        for (Client c: bank.getClients()) {
            System.out.println("Client " + c + " has balance " + c.getAccount().getBalance());
        }
    }

    public static void main(String[] args) {
        File file = new File(args[1]);
        Bank bank = new Bank();
        if (args[0].equals("-loadfeed") && args[1] != null) {
            BankDataLoaderService.loadBank(bank, file);
            BankService.saveBank(bank);
        }
        if (args[2].equals("-server")) {
            BankServer.startServer(bank);
        }
//        Bank bank = new Bank();
//        BankApplication.createAccounts(bank);
//        BankApplication.modifyBank(bank);
//        BankApplication.printBalance(bank);
//        BankService.printMaximumAmountToWithdraw(bank);

    }
}
